package com.example.supernote.data.network.config

import kotlinx.serialization.Serializable
import java.net.URLEncoder
import kotlin.math.round

fun encodeUrl(url: String): String {
    return URLEncoder.encode(url, "UTF-8")
}

// URL参数
fun getUrlParams(
    audioId: String = "",
    xSessionId: String = "",
    sliceIndex: String = ""
): Map<String, String> = mapOf(
    "client_version" to encodeUrl("unknown"),
    "package" to encodeUrl("unknown"),
    "user_id" to encodeUrl("2addc42b7ae689dfdf1c63e220df52a2"),
    "system_time" to encodeUrl(round(System.currentTimeMillis().toDouble()).toInt().toString()),
    "engineid" to "fileasrrecorder",
    "audio_id" to audioId,
    "x-sessionId" to xSessionId,
    "slice_index" to sliceIndex
)

fun convertMapToString(params: Map<String, String>): String {
    var str = ""
    if (params.isNotEmpty())  {
        params.keys.forEachIndexed { index, s ->
            if (index != 0) {
                str = "$str&"
            }
            str = "$str$s=${params[s]}"
        }
    }
    println("str: $str")
    return str
}

// 创建音频
class CreateAudioBody(
    private val audioType: String,
    private val xSessionId: String,
    private val sliceNum: String
) {
    fun getMap(): Map<String, String> = mapOf(
        "audio_type" to audioType,
        "x-sessionId" to xSessionId,
        "slice_num" to sliceNum
    )
}

@Serializable
data class CreateAudioResponse(
    val sid: String = "",
    val action: String = "",
    val data: MutableMap<String, String> = mutableMapOf("audio_id" to "", "x-sessionId" to ""),
    val code: Int = -1,
    val desc: String = "",
    val type: String = "",
)

// 分片上传
@Serializable
data class UploadAudioResponseData(
    val url: String = "",
    val total: Int = -1,
    val slices: Int = -1,
    val audio_id: String = ""
)

@Serializable
data class UploadAudioResponse (
    val sid: String = "",
    val action: String = "",
    val data: UploadAudioResponseData = UploadAudioResponseData(),
    val code: Int = -1,
    val desc: String = "",
    val type: String = ""
)

// 创建转写任务并运行
class RunAudioBody(
    private val audioId: String,
    private val xSessionId: String,
) {
    fun getMap() = mapOf(
        "audio_id" to audioId,
        "x-sessionId" to xSessionId
    )
}

@Serializable
data class RunAudioResponse (
    val sid: String = "",
    val action: String = "",
    val data: Map<String, String> = mapOf("task_id" to ""),
    val code: Int = -1,
    val desc: String = "",
    val type: String = ""
)

// 查询转写进度
class ProgressAudioBody(
    private val taskId: String,
    private val xSessionId: String,
) {
    fun getMap() = mapOf(
        "task_id" to taskId,
        "x-sessionId" to xSessionId
    )
}

@Serializable
data class ProgressAudioResponse (
    val sid: String = "",
    val action: String = "",
    val data: Map<String, Int> = mapOf("progress" to -1),
    val code: Int = -1,
    val desc: String = "",
    val type: String = ""
)

// 查询转写结果
class ResultAudioBody(
    private val taskId: String,
    private val xSessionId: String,
) {
    fun getMap() = mapOf(
        "task_id" to taskId,
        "x-sessionId" to xSessionId
    )
}

@Serializable
data class ResultData (
    val onebest: String = "",
    val bg: Int = -1,
    val ed: Int = -1,
    val speaker: Int = -1
)

@Serializable
data class ResultAudioResponse (
    val sid: String = "",
    val action: String = "",
    val data: Map<String, List<ResultData>> = mapOf("result" to listOf(ResultData())),
    val code: Int = -1,
    val desc: String = "",
    val type: String = ""
)